<?php

namespace App\Http\Controllers;

use App\Models\DoctorWorks;
use App\Models\WithdrawalLog;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\BaseController;

class WithdrawalLogController extends BaseController
{
    /***
     *
     * 申请提现
     */
    public function addWithdrawal(Request $request)
    {
        $param = $request->input();
        if (empty($param['money'])) {
            return $this->ResultJson(0, '金额不能为空', []);
        }
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, '用户ID不能为空', []);
        }
        if (empty($param['type'])) {
            return $this->ResultJson(0, '类型不能为空', []);
        }
        if ($param['type'] == 1) {
            $work = (new DoctorWorks())->where([['id', '=', $param['user_id']]])->first();
            if(empty($work)){
                return $this->ResultJson(0, '该工作室不存在', []);
            }
            if ($work->surplus < $param['money']) {
                return $this->ResultJson(0, '您的提现余额不足', []);
            }
        } elseif ($param['type'] == 2) {
            $work = (new User())->where([['id', '=', $param['user_id']]])->first();
            if(empty($work)){
                return $this->ResultJson(0, '该用户不存在', []);
            }
            if ($work->surplus < $param['money']) {
                return $this->ResultJson(0, '您的提现余额不足', []);
            }
        }
        if ($param['money'] < 1) {
            return $this->ResultJson(0, '提现金额不能小于1', []);
        }
        $data = (new WithdrawalLog())->add($param);
        if($data){
            return $this->ResultJson(1, '申请成功!', []);
        }
        return $this->ResultJson(0, '申请失败', []);
    }

}
